package com.hmdp.controller;


import cn.hutool.json.JSONUtil;
import com.hmdp.dto.Result;
import com.hmdp.entity.ShopType;
import com.hmdp.service.IShopTypeService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @since 2021-12-22
 */
@RestController
@RequestMapping("/shop-type")
public class ShopTypeController {
    @Resource
    private IShopTypeService typeService;

    @Resource
    private StringRedisTemplate stringRedisTemplate;


    @GetMapping("list")
    public Result queryTypeList() {
        List<String> shopTypeList1 = stringRedisTemplate.opsForList().range("shopType", 0, 10);
        if (shopTypeList1 == null || shopTypeList1.isEmpty()) {
            List<ShopType> shopTypeList2 = typeService
                    .query().orderByAsc("sort").list();
            for (ShopType a:shopTypeList2 ) {
                String s = JSONUtil.toJsonStr(a);
                stringRedisTemplate.opsForList().rightPush("shopType",s);
            }
            return Result.ok(shopTypeList2);}
        else{
           List<ShopType> list=new ArrayList<>();
            for (String a:shopTypeList1) {
                ShopType shopType = JSONUtil.toBean(a, ShopType.class);
                list.add(shopType);
            }
              return Result.ok(list);}
        }
    }
//        List<ShopType> typeList = typeService
//                .query().orderByAsc("sort").list();
//        return Result.ok(typeList);
